package com.suineg.pms.web.account;

import java.util.List;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.suineg.pms.entity.Role;
import com.suineg.pms.service.account.AccountService;

/**
 * 角色管理
 *@author Suine
 */
@Controller
@RequestMapping(value = "/admin/role")
public class RoleController {
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private AccountService accountService;
	
	@RequestMapping(method = RequestMethod.GET)
	@RequiresPermissions("admin")
	public String list(Model model){
		List<Role> roleList = accountService.getAllRole(); 
		model.addAttribute("roleList", roleList);
		return "account/adminRoleList";
	}
	
	@RequestMapping(value = "update/{id}",method = RequestMethod.GET)
	@RequiresPermissions("admin")
	public String updateForm(@PathVariable("id") Long id,Model model){
		model.addAttribute("role", accountService.getRole(id));
		model.addAttribute("action", "update");
		return "account/adminRoleForm";
	}
	
	@RequestMapping(value = "update",method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("preloadRole") Role role,RedirectAttributes redirectAttributes){
		accountService.saveRole(role);
		redirectAttributes.addFlashAttribute("message", "更新" + role.getName() + "成功");
		return "redirect:/admin/role";
	}
	
	@RequestMapping(value = "saveAdd",method = RequestMethod.POST)
	public String saveAdd(@Valid Role role,RedirectAttributes redirectAttributes){
		accountService.saveRole(role);
		redirectAttributes.addFlashAttribute("message", "创建" + role.getName() + "成功");
		return "redirect:/admin/role";
	}
	
	@SuppressWarnings("unused")
	@RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
	public String delete(@PathVariable("id") Long id,RedirectAttributes redirectAttributes){
		Role role = accountService.getRole(id);
		String name = role.getName();
		if(role == null){
			redirectAttributes.addFlashAttribute("message", "ID:[" + id + "]不存在");
		}else{
			accountService.deleteRole(id);
			redirectAttributes.addFlashAttribute("message", "删除角色" + name + "成功");
		}
		return "redirect:/admin/role";
	}
	
	@RequestMapping(value = "add")
	public String addForm(Model model){
		model.addAttribute("action", "saveAdd");
		return "account/adminRoleForm";
	}
	
	/**
	 * 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Role对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preloadRole")
	public Role getRole(@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			return accountService.getRole(id);
		}
		return null;
	}
	
	@RequestMapping(value = "checkName")
	@ResponseBody
	public String ckeckName(Model model,@RequestParam("name") String name,@RequestParam("oldName") String oldName){
		if(name.equals(oldName)){
			return "true";
		}else if(accountService.findRoleByRoleName(name) == null){
			return "true";
		}
		return "false";
	}

	@Autowired
	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}
}

